
const state = {
    setupCount: 888,
    todoList: [
        {
            id: 1,status: true,
        },
        {
            id: 2,status: false,
        }
    ],
    a: 111,
    b: 222,
}

const mutations = {
    increment(state) {
        //对于模块内部的mutation和getter，接收的参数是模块的局部状态对象。
        state.setupCount++;
    }
}

const getters = {
    //state 模块的局部状态
    filterTodoList: (state,getters,rootState)=>{
        console.log('---filterTodoList---');
        console.log('getters',getters);
        console.log('rootState',rootState);
        return state.todoList.filter((item)=> item.status);
    }
}

const actions = {
    oddRootSum({ state,commit,rootState}) {
        console.log('oddRootSum');
        console.log('state---',state);
        console.log('commit---',commit);
        console.log('rootState---',rootState);
        commit('increment');
    },
    foo({commit}) {
        commit('increment');
    },
    bar({commit}) {
        commit('increment');
    }
}

const modules = {
    //带命名空间的模块
    namespaced: true,
    myPage: {
        state: {
            myPageCount: 888,
            myPageList: [
                {
                    name: '天下',
                    status: true,
                },
                {
                    name: '小小',
                    status: false,
                }
            ]
        },
        getters: {
            filterMyPageList(state) {
                //console.log('filterMyPageList',state);
                return state.myPageList.filter((item)=> item.status)
            },
            someGetter(state,getters,rootState,rootGetters) {
                //console.log('---someGetter---');
                //console.log('state',state);
                //console.log('getters',getters);
                //console.log('rootState',rootState);
                //console.log('rootGetters',rootGetters);
                return getters.filterMyPageList;
            }
        },
        actions: {
            someAction({ dispatch,commit,getters,rootGetters }) {
                console.log('---someAction---');
                //console.log('dispatch',dispatch);
                //console.log('commit',commit);
                console.log('getters',getters);
                console.log('rootGetters',rootGetters);
                //dispatch('oddRootSum');
                dispatch('action1226',null,{ root: true });
            },
            someOtherAction({commit}) {
                commit('increment');
            },
            //在带命名空间的模块注册全局的action，你可以添加root: true,并将这个action的定义放在handler中
            handlerAction: {
                root: true,
                handler(namespacedContext,payload) {
                    console.log('namespacedContext',namespacedContext);
                    console.log('payload',payload);
                }
            }
        }
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    getters,
    actions,
    modules
}